home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 52856 / 52856.xpi / modules / datastore.jsm
Text File  |  2009-12-03  |  3KB  |  121 lines

  1. var EXPORTED_SYMBOLS = ["HandyTabDataStore"];
  2.  
  3. var HandyTabDataStore = {
  4.   initdone: false,
  5.   file: null,
  6.   marks: [],
  7.   newMarks: [],
  8.   dirty: false,
  9.  
  10.   init: function() {
  11.     if (this.initdone) return;
  12.     this.initdone = true;
  13.     
  14.     var f = Components.classes["@mozilla.org/file/directory_service;1"]
  15.                       .getService(Components.interfaces.nsIProperties)
  16.                       .get("ProfD", Components.interfaces.nsIFile);
  17.  
  18.     f.append("handytab");
  19.     if (!f.exists() || !f.isDirectory()) {
  20.       f.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0700);
  21.     }
  22.     f.append("marks.json");
  23.     
  24.     this.file = f;
  25.   },
  26.   
  27.   getMarks: function(all) {
  28.     this.marks = this.marks.concat(this.newMarks);
  29.     var str = (all ? JSON.stringify(this.marks) : JSON.stringify(this.newMarks));
  30.     this.newMarks = [];
  31.  
  32.     return str;
  33.   },
  34.   
  35.   addMark: function(mark) {
  36.     this.dirty = true;
  37.     this.newMarks.push(mark);
  38.   },
  39.   
  40.   removeMarks: function(items) {
  41.     if (items == null) {
  42.       if (this.marks.length == 0 && this.newMarks.length == 0) return;
  43.       this.marks = [];
  44.       this.newMarks = [];
  45.       this.dirty = true;
  46.       return;
  47.     }
  48.     
  49.     if (items.length == 0) return;
  50.     
  51.     items = items.split(",");
  52.     for (var i = 0; i < items.length; i++) {
  53.       var index = parseInt(items[i]);
  54.       this.marks.splice(index, 1);
  55.       this.dirty = true;
  56.     }
  57.   },
  58.   
  59.   reset: function() {
  60.     this.marks = [];
  61.     this.newMarks = [];
  62.     this.dirty = false;
  63.     
  64.     if (this.file.exists()) this.file.remove(false);
  65.   },
  66.   
  67.   save: function(removeOnly) {
  68.     if (!this.dirty) return;
  69.     this.dirty = false;
  70.     
  71.     this.marks = this.marks.concat(this.newMarks);
  72.     this.newMarks = [];
  73.     
  74.     var fstream = Components.classes["@mozilla.org/network/file-output-stream;1"]
  75.                             .createInstance(Components.interfaces.nsIFileOutputStream);
  76.     var cstream = Components.classes["@mozilla.org/intl/converter-output-stream;1"]
  77.                             .createInstance(Components.interfaces.nsIConverterOutputStream);
  78.  
  79.     fstream.init(this.file, 0x02 | 0x08 | 0x20, 0600, 0); 
  80.     cstream.init(fstream, "UTF-8", 0, 0);
  81.  
  82.     var savestr = JSON.stringify(this.marks);
  83.     cstream.writeString(savestr);
  84.  
  85.     cstream.close();
  86.     fstream.close();
  87.   },
  88.   
  89.   load: function() {
  90.     this.marks = [];
  91.     this.dirty = false;
  92.  
  93.     if (!this.file.exists()) return;
  94.     
  95.     var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"]
  96.                             .createInstance(Components.interfaces.nsIFileInputStream);
  97.     var cstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"]
  98.                             .createInstance(Components.interfaces.nsIConverterInputStream);
  99.  
  100.     fstream.init(this.file, -1, 0, 0);
  101.     cstream.init(fstream, "UTF-8", 0, 0);
  102.  
  103.     var loadstr = "";
  104.     
  105.     var data = {};
  106.     cstream.readString(-1, data);
  107.     loadstr = data.value;
  108.  
  109.     cstream.close();
  110.     fstream.close();
  111.     
  112.     try {
  113.       this.marks = JSON.parse(loadstr);
  114.     } catch (e) {
  115.       dump("handytab: failed to parse data file!\n");
  116.       this.file.copyTo(null, "marks.json.failed.bak");
  117.     }
  118.   }
  119.   
  120. };
  121.